/**
 * @file lv_port_indev_templ.c
 *
 */

 /*Copy this file as "lv_port_indev.c" and set this value to "1" to enable content*/
#if 1

/*********************
 *      INCLUDES
 *********************/
#include "lv_port_indev.h"
#include "../../lvgl.h"
#include "debug.h"

/*********************
 *      DEFINES
 *********************/

/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/

static void keypad_init(void);
static void keypad_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data);
static uint32_t keypad_get_key(void);



/**********************
 *  STATIC VARIABLES
 **********************/

lv_indev_t * indev_keypad;


/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
 **********************/

void lv_port_indev_init(void)
{
    /**
     * Here you will find example implementation of input devices supported by LittelvGL:
     *  - Touchpad
     *  - Mouse (with cursor support)
     *  - Keypad (supports GUI usage only with key)
     *  - Encoder (supports GUI usage only with: left, right, push)
     *  - Button (external buttons to press points on the screen)
     *
     *  The `..._read()` function are only examples.
     *  You should shape them according to your hardware
     */

    static lv_indev_drv_t indev_drv;

    /*------------------
     * Keypad
     * -----------------*/

    /*Initialize your keypad or keyboard if you have*/
    keypad_init();

    /*Register a keypad input device*/
    lv_indev_drv_init(&indev_drv);
    indev_drv.type = LV_INDEV_TYPE_KEYPAD;
    indev_drv.read_cb = keypad_read;
    indev_keypad = lv_indev_drv_register(&indev_drv);

    /*Later you should create group(s) with `lv_group_t * group = lv_group_create()`,
     *add objects to the group with `lv_group_add_obj(group, obj)`
     *and assign this input device to group to navigate in it:
     *`lv_indev_set_group(indev_keypad, group);`*/

}

/**********************
 *   STATIC FUNCTIONS
 **********************/

/*------------------
 * Keypad
 * -----------------*/

/*Initialize your keypad*/
static void keypad_init(void)
{
    /*Your code comes here*/
}

/*Will be called by the library to read the mouse*/
static void keypad_read(lv_indev_drv_t * indev_drv, lv_indev_data_t * data)
{
    static uint32_t last_key = 0;

    /*Get the current x and y coordinates*/
    //mouse_get_xy(&data->point.x, &data->point.y);
	data->point.x =0;
	data->point.y =0;
    /*Get whether the a key is pressed and save the pressed key*/
    uint32_t act_key = keypad_get_key();
#if LVGL_INPUT_DEV_DEBUG
	DEBUG(3,"keypadval:%d",act_key);
#endif
    if(act_key != 0) {
        data->state = LV_INDEV_STATE_PR;

        /*Translate the keys to LVGL control characters according to your key definitions*/
//        switch(act_key) {
//            case 1:
//                act_key = LV_KEY_DOWN;
//                break;
//            case 2:
//                act_key = LV_KEY_UP;
//                break;
//            case 3:
//                act_key = LV_KEY_LEFT;
//                break;
//            case 4:
//                act_key = LV_KEY_RIGHT;
//                break;
//            case 5:
//                act_key = LV_KEY_ENTER;
//                break;
//        }

        last_key = act_key;
    }
    else {
        data->state = LV_INDEV_STATE_REL;
    }

    data->key = last_key;
}

#include "ADC.h"
#include "queue.h"
#include "semphr.h"
/*Get the currently being pressed key.  0 if no key is pressed*/
extern xQueueHandle		ADC_DataQueue;
static uint32_t keypad_get_key(void)
{
    /*Your code comes here*/
#if (ADC_IRQ_ENABLE	== 1)
	ADC_VAL ad_val;
	xQueueReceive(ADC_DataQueue, &ad_val,0);
	if(key5_to_lvgl_flag)
	{
		if(ad_val.key5 < 200)	//press   7
			return LV_KEY_ENTER;
		else if(ad_val.key5  <1600)	//right  1389
			return LV_KEY_PREV;
		else if(ad_val.key5  <2300)	//prev 2069
			return LV_KEY_LEFT;
		else if(ad_val.key5  < 3000)	//left 2719
			return LV_KEY_NEXT;
		else if (ad_val.key5  < 3500)	//next 3232
			return LV_KEY_RIGHT;
	}
#else
	cla_avg();
	if(key5_to_lvgl_flag)
	{
		if(key5_adval < 200)	//press   7
			return LV_KEY_ENTER;
		else if(key5_adval <1600)	//right  1389
			return LV_KEY_PREV;
		else if(key5_adval <2300)	//prev 2069
			return LV_KEY_LEFT;
		else if(key5_adval < 3000)	//left 2719
			return LV_KEY_NEXT;
		else if (key5_adval < 3500)	//next 3232
			return LV_KEY_RIGHT;
	}
#endif
	
	if(BACK_KEY)	//back   
		return LV_KEY_ESC;
	
    return 0;
}

#else /*Enable this file at the top*/

/*This dummy typedef exists purely to silence -Wpedantic.*/
typedef int keep_pedantic_happy;
#endif
